package com.jaws.core.mvc.exception;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * 返回状态码(错误码)
 * <p>
 * 业务模块
 * <ul>
 * <li>公共模块: 100,公共模块错误码: 100000~100999</li>
 * <li>用户模块: 200,用户模块错误码: 200000~200999</li>
 * <li>账户模块: 300,账户模块错误码: 300000~300999</li>
 * <li>产品模块: 400,产品模块错误码: 400000~400999</li>
 * <li>结算模块: 410,结算模块错误码: 410000~410999</li>
 * <li>支付模块: 500,支付模块错误码: 500000~500999</li>
 * <li>文件服務模块: 510,文件服務模块错误码: 510000~510999</li>
 * <li>活动模块: 600,活动模块错误码: 600000~600999</li>
 * <li>消息模块: 700,消息模块错误码: 700000~700999</li>
 * <li>前端模块: 800,</li>
 * <li>友金社模块: 900,</li>
 * </ul>
 * </p>
 * 
 * @author jiangmy
 * @date 2016-08-16 19:15:09
 * @since v1.0.0
 */
@SuppressWarnings("serial")
public class RspCodeEnum extends BaseEnum<Integer> {
	// 枚举定义
	public static final Map<String, RspCodeEnum> values = new LinkedHashMap<String, RspCodeEnum>();
	// 枚举key,用于在数据库加载
	public static final String ENUM_KEY = "RSP_CODE";

	public static final RspCodeEnum SUCCESS = build("SUCCESS", 0, "", "");
	// common
	public static final RspCodeEnum COMMON_ERROR_PAGE_NOT_FOUND = build("COMMON_ERROR_S_NOT_FOUND", 100000, "服务不存在", "服务不存在");

	public static final RspCodeEnum COMMON_ERROR_PARAMETER_REQUIRED = RspCodeEnum
			.build("COMMON_ERROR_PARAMETER_REQUIRED", 100001, "关键参数缺失", "关键参数缺失");

	public static final RspCodeEnum COMMON_ERROR_PARAMETER_INVALID = build("COMMON_ERROR_PARAMETER_INVALID", 100002, "参数不正确", "参数不正确");

	public static final RspCodeEnum COMMON_ERROR_SQL_EXCEPTION = build("COMMON_ERROR_SQL_EXCEPTION", 100003, "数据库异常", "写数据异常");

	public static final RspCodeEnum COMMON_ERROR_FORMAT_EXCEPTION = build("COMMON_ERROR_FORMAT_EXCEPTION", 100004, "数据格式异常", "数据格式异常");

	public static final RspCodeEnum COMMON_ERROR_NO_USER_AUTHORIZED = build("COMMON_ERROR_NO_USER_AUTHORIZED", 100005, "用户未登录", "用户未登录");

	public static final RspCodeEnum COMMON_ERROR_OBJECT_NOT_FOUND = build("COMMON_ERROR_OBJECT_NOT_FOUND", 100006, "查询目标不存在", "查询目标不存在");

	public static final RspCodeEnum COMMON_ERROR_NO_SIGN_AUTHENTICATION = build("COMMON_ERROR_NO_SIGN_AUTHENTICATION", 100007, "接口签名不正确", "接口签名不正确");

	public static final RspCodeEnum COMMON_ERROR_UPDATE_FAILED = build("COMMON_ERROR_UPDATE_FAILED", 100008, "更新数据失败", "更新数据失败");

	public static final RspCodeEnum COMMON_ERROR_BAD_REQUEST = build("COMMON_ERROR_BAD_REQUEST", 100009, "无效请求", "无效请求");

	public static final RspCodeEnum COMMON_ERROR_NO_BIND_USER = build("COMMON_ERROR_NO_BIND_USER", 100010, "没有绑定用户", "没有绑定用户");

	public static final RspCodeEnum COMMON_ERROR_UNKNOWN_CHANNEL_ID = build("COMMON_ERROR_UNKNOWN_CHANNEL_ID", 100011, "未知渠道", "未知渠道");

	public static final RspCodeEnum COMMON_ERROR_PHONE_BINDED_OTHER = build("COMMON_ERROR_PHONE_BINDED_OTHER", 100012, "该手机号已被其他用户绑定", "该手机号已被其他用户绑定");

	public static final RspCodeEnum COMMON_ERROR_EMAIL_BINDED_OTHER = build("COMMON_ERROR_EMAIL_BINDED_OTHER", 100013, "该邮箱已被其他用户绑定", "该邮箱已被其他用户绑定");

	public static final RspCodeEnum COMMON_ERROR_PHONE_BINDED = build("COMMON_ERROR_PHONE_BINDED", 100014, "已绑定手机用户，请调用改绑手机接口", "已绑定手机用户，请调用改绑手机接口");

	public static final RspCodeEnum COMMON_ERROR_EMAIL_BINDED = build("COMMON_ERROR_EMAIL_BINDED", 100015, "已绑定邮箱用户，请调用改绑邮箱接口", "已绑定邮箱用户，请调用改绑邮箱接口");

	public static final RspCodeEnum COMMON_ERROR_UNKNOWN_EXCEPTION = build("UNKNOWN_EXCEPTION", 100016, "未处理的异常，系统出错", "系统出错");

	public static final RspCodeEnum COMMON_ERR0R_NET_ERROR = build("COMMON_ERR0R_NET_ERROR", 100017, "网络异常", "网络异常");
	
	public static final RspCodeEnum COMMON_ERR0R_DUPLICATE_OPT = build("COMMON_ERR0R_DUPLICATE_OPT", 100018, "操作频繁，请稍后再试", "操作频繁，请稍后再试");

	// user
	public static final RspCodeEnum USER_AUTH_EXISTS = build("USER_AUTH_EXISTS", 200003, "用户已认证", "用户已认证");

	public static final RspCodeEnum USER_EXISTS = build("USER_EXISTS", 200004, "用户已存在", "用户已存在");

	public static final RspCodeEnum USER_NOT_EXISTS = build("USER_NOT_EXISTS", 200005, "用户不存在", "用户不存在");

	public static final RspCodeEnum USER_VERIFY_CODE_ERROR = build("USER_VERIFY_CODE_ERROR", 200006, "验证码不正确", "验证码不正确");

	public static final RspCodeEnum USER_PASSWORD_ERROR = build("USER_PASSWORD_ERROR", 200007, "密码不正确", "账号或密码不正确");

	public static final RspCodeEnum USER_PASSWORD_ERROR_TIMES = RspCodeEnum
			.build("USER_PASSWORD_ERROR_TIMES", 200008, "密码错误次数达到每日上限", "密码错误次数达到每日上限");

	public static final RspCodeEnum USER_REG_FAILED = build("USER_REG_FAILED", 200009, "注册失败", "注册失败");

	public static final RspCodeEnum TAG_NOT_EXISTS = build("TAG_NOT_EXISTS", 200010, "标签不存在", "标签不存在");

	public static final RspCodeEnum CHANNEL_BIND_YYFAX_CONFLICT = build("CHANNEL_BIND_YYFAX_CONFLICT", 200011, "绑定关系冲突,友金所用户已绑定", "绑定关系冲突");

	public static final RspCodeEnum CHANNEL_BIND_OTHER_CONFLICT = RspCodeEnum
			.build("CHANNEL_BIND_OTHER_CONFLICT", 200012, "绑定关系冲突,渠道用户已绑定", "绑定关系冲突");

	public static final RspCodeEnum USER_LOCKED = build("USER_LOCKED", 200013, "用户被锁定", "用户被锁定");

	public static final RspCodeEnum USER_CANCEL_FAILURE = build("USER_CANCEL_FAILURE", 200014, "注销用户失败", "注销用户失败");
	
	public static final RspCodeEnum SIGMA_SYNC_USER_INFO_ERROR = build("SIGMA_SYNC_USER_INFO_ERROR", 200015, "同步用户信息失败", "同步用户信息失败");
	// 活动模块

	/** 活动-积分兑换异常-积分不足 */
	public static final RspCodeEnum ACTIVITY_POINT_EXCHAGNE_LACK_POINT = build("ACTIVITY_POINT_EXCHAGNE_LACK_POINT", 600001, "积分不足，兑换失败", "积分不足，兑换失败");

	public static final RspCodeEnum MSG_RECEIVE_FAIL = build("MSG_RECEIVE_FAIL", 700000, "消息接收失败", "消息接收失败");
	
	public static final RspCodeEnum MSG_SEND_COUNT_LIMIT = build("MSG_SEND_COUNT_LIMIT", 700001, "短信次数已达上限", "短信次数已达当日上限");
	
	public static final RspCodeEnum MSG_SEND_FREQUNTLY = build("MSG_SEND_FREQUNTLY", 700002, "消息发送间隔太短", "消息发送间隔太短");

	protected String display;

	// 构造函数
	public RspCodeEnum(String name, int value, String remark, String display) {
		super(name, value, remark);
		this.display = display;
	}

	public String getDisplay() {
		return display;
	}

	public static List<RspCodeEnum> values() {
		return new ArrayList<RspCodeEnum>(values.values());
	}

	/**
	 * 不存在则添加,存在则更新(用于数据库加载时)
	 * 
	 * @author jiangmy
	 * @date 2016-12-05 19:31:30
	 * @param name
	 * @param value
	 * @param remark
	 * @return
	 */
	public static final RspCodeEnum build(String name, int value, String remark, String display) {
		RspCodeEnum e = values.get(name);
		if (e == null) {
			e = new RspCodeEnum(name, value, remark, display);
			values.put(name, e);
		} else {
			e.value = value;
			e.remark = remark;
		}
		return e;
	}

	public static final RspCodeEnum fromValue(int value) {
		for (RspCodeEnum e : values.values()) {
			if (value == e.getValue()) {
				return e;
			}
		}
		return null;
	}

	public static final RspCodeEnum fromName(String name) {
		for (RspCodeEnum e : values.values()) {
			if (StringUtils.equalsIgnoreCase(name, e.getName())) {
				return e;
			}
		}
		return null;
	}

	public static void main(String[] args) {
		for (RspCodeEnum r : RspCodeEnum.values()) {
			System.out.println(r.getValue() + " | " + r.getName() + " | " + r.getRemark() + " | " + r.getDisplay());
		}
	}
}
